#!/bin/sh
#error and exit = set -e
#set -o errexit
#color
source /etc/init.d/functions
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'

function ulimitConf(){
  cat >> /etc/security/limits.conf <<\EOF
# Set limits for processes
*          soft    nproc     1048576
*          hard    nproc     1048576
root       soft    nproc     unlimited

# Set limits for open files
*          soft    nofile    1048576
*          hard    nofile    1048576

# Set limits for memory locking (only needed if using mlock or mlockall)
*          soft    memlock   unlimited
*          hard    memlock   unlimited

# Set limits for real-time priority (only needed if using sched_setscheduler or sched_setparam)
# *          soft    rtprio    0
# *          hard    rtprio    0
EOF
}

function kernelConf(){
  cat > /etc/sysctl.d/k8s.conf <<EOF
# 内核参数
kernel.softlockup_panic = 1
kernel.softlockup_all_cpu_backtrace = 1

# 虚拟内存参数
vm.swappiness = 0
vm.overcommit_memory = 1
vm.panic_on_oom = 0
vm.max_map_count = 262144

# 文件系统参数
fs.inotify.max_user_watches = 524288
fs.may_detach_mounts = 1
fs.file-max = 1048576
fs.nr_open = 1048576

# 网络参数
# IPv4 参数
net.ipv4.tcp_keepalive_time = 7200
net.ipv4.tcp_keepalive_intvl = 75
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.neigh.default.gc_stale_time = 120
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_synack_retries = 2
net.ipv4.ip_forward=1
net.core.netdev_max_backlog=1000
net.core.somaxconn=32768
net.core.rmem_max=16777216
net.core.wmem_max=16777216
net.ipv4.tcp_wmem=4096 87380 16777216
net.ipv4.tcp_rmem=4096 87380 16777216
net.ipv4.tcp_max_syn_backlog=8096
net.ipv4.tcp_slow_start_after_idle=0
net.ipv4.tcp_tw_reuse=1

# IPv6 参数
net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
net.ipv6.conf.lo.disable_ipv6=1

# 桥接参数
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-arptables=1

# 连接跟踪参数
net.netfilter.nf_conntrack_max=1048576
net.netfilter.nf_conntrack_buckets=262144

# 其他网络参数
net.core.netdev_max_backlog=1000
net.core.somaxconn=32768
net.core.rmem_max=16777216
net.core.wmem_max=16777216
net.ip_local_reserved_ports=30000-32767
EOF
}

function ipvsConf(){
  cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
# Load modules for ipvs load balancing
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh

# Load modules for connection tracking and firewall rules
modprobe -- nf_conntrack
modprobe -- nf_conntrack_netlink
modprobe -- ipt_rpfilter
modprobe -- ip_tables
modprobe -- ip6_tables
modprobe -- ipt_set
modprobe -- ipt_REJECT

# Load modules for ipset support
modprobe -- ip_set
modprobe -- xt_set

# Load modules for calico network plugin (only needed for ipip mode)
modprobe -- ipip
modprobe -- tun

# Load module for ceph rbd device (only needed if using rbd)
modprobe -- rbd

# Load module for vxlan device (only needed if using overlay network or flannel plugin)
# modprobe -- vxlan

modprobe -- overlay
modprobe -- br_netfilter
EOF

chmod 755 /etc/sysconfig/modules/ipvs.modules

}



function deal(){
  ulimitConf
  kernelConf
  ipvsConf
}


function main(){
  action "$0" deal
}

main $*
